package com.edgegateway.web.controller.iot;

import java.util.List;

import com.edgegateway.common.core.domain.entity.SysUser;
import com.edgegateway.common.pk.IdWorker;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.edgegateway.common.annotation.Log;
import com.edgegateway.common.enums.BusinessType;
import com.edgegateway.system.domain.IotThingsModelTemplate;
import com.edgegateway.system.service.IIotThingsModelTemplateService;
import com.edgegateway.common.core.controller.BaseController;
import com.edgegateway.common.core.domain.AjaxResult;
import com.edgegateway.common.utils.poi.ExcelUtil;
import com.edgegateway.common.core.page.TableDataInfo;

/**
 * 通用物模型Controller
 * 
 * @author Doooxu
 * @date 2023-01-11
 */
@Controller
@RequestMapping("/system/template")
public class IotThingsModelTemplateController extends BaseController
{
    private String prefix = "iot/template";

    @Autowired
    private IIotThingsModelTemplateService iotThingsModelTemplateService;

    @Autowired
    private IdWorker idWorker;

    @RequiresPermissions("system:template:view")
    @GetMapping()
    public String template()
    {
        return prefix + "/template";
    }

    /**
     * 查询通用物模型列表
     */
    @RequiresPermissions("system:template:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(IotThingsModelTemplate iotThingsModelTemplate)
    {
        startPage();
        List<IotThingsModelTemplate> list = iotThingsModelTemplateService.selectIotThingsModelTemplateList(iotThingsModelTemplate);
        return getDataTable(list);
    }

    /**
     * 导出通用物模型列表
     */
    @RequiresPermissions("system:template:export")
    @Log(title = "通用物模型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(IotThingsModelTemplate iotThingsModelTemplate)
    {
        List<IotThingsModelTemplate> list = iotThingsModelTemplateService.selectIotThingsModelTemplateList(iotThingsModelTemplate);
        ExcelUtil<IotThingsModelTemplate> util = new ExcelUtil<IotThingsModelTemplate>(IotThingsModelTemplate.class);
        return util.exportExcel(list, "通用物模型数据");
    }

    /**
     * 新增通用物模型
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存通用物模型
     */
    @RequiresPermissions("system:template:add")
    @Log(title = "通用物模型", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(IotThingsModelTemplate iotThingsModelTemplate)
    {
        iotThingsModelTemplate.setTemplateId(idWorker.nextId());
        return toAjax(iotThingsModelTemplateService.insertIotThingsModelTemplate(iotThingsModelTemplate));
    }

    /**
     * 修改通用物模型
     */
    @RequiresPermissions("system:template:edit")
    @GetMapping("/edit/{templateId}")
    public String edit(@PathVariable("templateId") String templateId, ModelMap mmap)
    {
        IotThingsModelTemplate iotThingsModelTemplate = iotThingsModelTemplateService.selectIotThingsModelTemplateByTemplateId(templateId);
        mmap.put("iotThingsModelTemplate", iotThingsModelTemplate);
        return prefix + "/edit";
    }

    /**
     * 修改保存通用物模型
     */
    @RequiresPermissions("system:template:edit")
    @Log(title = "通用物模型", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(IotThingsModelTemplate iotThingsModelTemplate)
    {
        return toAjax(iotThingsModelTemplateService.updateIotThingsModelTemplate(iotThingsModelTemplate));
    }

    /**
     * 删除通用物模型
     */
    @RequiresPermissions("system:template:remove")
    @Log(title = "通用物模型", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(iotThingsModelTemplateService.deleteIotThingsModelTemplateByTemplateIds(ids));
    }

    /**
     * 系统通用状态修改
     */
    @Log(title = "通用物模型", businessType = BusinessType.UPDATE)
    @RequiresPermissions("system:template:edit")
    @PostMapping("/changeIsSys")
    @ResponseBody
    public AjaxResult changeIsSys(IotThingsModelTemplate iotThingsModelTemplate)
    {
        IotThingsModelTemplate template = iotThingsModelTemplateService.selectIotThingsModelTemplateByTemplateId(iotThingsModelTemplate.getTemplateId());
        template.setIsSys(iotThingsModelTemplate.getIsSys());
        return toAjax(iotThingsModelTemplateService.updateIotThingsModelTemplate(template));
    }

    /**
     * 首页显示状态修改
     */
    @Log(title = "通用物模型", businessType = BusinessType.UPDATE)
    @RequiresPermissions("system:template:edit")
    @PostMapping("/changeIsTop")
    @ResponseBody
    public AjaxResult changeIsTop(IotThingsModelTemplate iotThingsModelTemplate)
    {
        IotThingsModelTemplate template = iotThingsModelTemplateService.selectIotThingsModelTemplateByTemplateId(iotThingsModelTemplate.getTemplateId());
        template.setIsTop(iotThingsModelTemplate.getIsTop());
        return toAjax(iotThingsModelTemplateService.updateIotThingsModelTemplate(template));
    }

    /**
     * 实时监测状态修改
     */
    @Log(title = "通用物模型", businessType = BusinessType.UPDATE)
    @RequiresPermissions("system:template:edit")
    @PostMapping("/changeIsMonitor")
    @ResponseBody
    public AjaxResult changeIsMonitor(IotThingsModelTemplate iotThingsModelTemplate)
    {
        IotThingsModelTemplate template = iotThingsModelTemplateService.selectIotThingsModelTemplateByTemplateId(iotThingsModelTemplate.getTemplateId());
        template.setIsMonitor(iotThingsModelTemplate.getIsMonitor());
        return toAjax(iotThingsModelTemplateService.updateIotThingsModelTemplate(template));
    }

}
